home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / dviselect / arith.h < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-16  |  2.2 KB  |  57 lines

  1. /*
  2.  * Copyright (c) 1987 University of Maryland Department of Computer Science.
  3.  * All rights reserved.  Permission to copy for any purpose is hereby granted
  4.  * so long as this copyright notice remains intact.
  5.  */
  6.  
  7. /*
  8.  * Arithmetic with scaled dimensions
  9.  *
  10.  * From `TEX.WEB' by D. E. Knuth:
  11.  *
  12.  * `The principal computations performed by TeX are done entirely in terms
  13.  * of integers less than 2^31 in magnitude; and divisions are done only when
  14.  * both divident and divisor are nonnegative.  Thus, the arithemtic specified
  15.  * in this program can be carried out in exactly the same way on a wide
  16.  * variety of computers, including some small ones.  Why?  Because the
  17.  * arithmetic calculations need to be spelled out precisely in order to
  18.  * guarantee that TeX will produce identical output on different machines.
  19.  * If some quantities were rounded differently in different implementations,
  20.  * we would find that line breaks and even page breaks might occur in
  21.  * different places.  Hence the arithmetic of TeX has been designed with
  22.  * care, and systems that claim be be implementations of TeX82 should follow
  23.  * precisely the calculations as they appear in the present program.'
  24.  *
  25.  * Thus, this follows the given implementation with few (no) optimizations.
  26.  */
  27.  
  28. /*
  29.  * We do fixed-point arithmetic on `scaled integers'.  These should
  30.  * be (at least) 32 bits.  Note also that it is assumed that certain
  31.  * `int' values may be stored in these (usually small, or else one
  32.  * of the magic 1<<n values).
  33.  */
  34. typedef i32 scaled;
  35.  
  36. #define UNITY      (1<<16)    /* 2^{16}, or 1.00000 */
  37. #define TWO      (1<<17)    /* 2^{17}, or 2.00000 */
  38. #define MAXSCALED ((1<<30)-1)    /* maximum scaled value, 2^{30}-1 */
  39.  
  40. /*
  41.  * This works on two's complement machines.  If you are not on one of those,
  42.  * you will need something different here.
  43.  */
  44. #define odd(n) ((n) & 1)
  45.  
  46. /* half of an integer, unambiguous with respect to signed odd numbers */
  47. #define half(x) (odd (x) ? ((x) + 1) / 2 : (x) / 2)
  48.  
  49. int ArithError;            /* set true whenever an arithmetic overflow
  50.                    occurs */
  51.  
  52. char *UnScale ();        /* return a pointer to the external
  53.                    representation of a `scaled'.
  54.                    (Alas, since I am returning it in
  55.                    static storage, you can only use
  56.                    it once without copying.) */
  57.